﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Data.SqlClient;

namespace HYWJ
{

    class QuestionCls
    {
        public static int QuestionIndex = 0; //当前试题编号
        public static int[] QID;
        public static int StuSubjectId = 0;   //学员所选课程ID
        public static int count = 0;                //计数器
        public static int timerHour = 1200; //考试时间单位(秒)
        public static string TimeValue = "";//时间的标准格式
        public static string[] Answers = new string[21];//学员选择的答案
        public static int stuTrueNum = 0;//学员答对题目数量
        public static Boolean ExamYesNo = false;//当前是否正在考试

        //数据初始化
        public static void initExam()
        {
            ExamYesNo = false;
            QuestionIndex = 0;
            StuSubjectId = 0;
            count = 0;
            timerHour = 1200;
            TimeValue = "";
            for (int i = 0; i < Answers.Length; i++)
            {
                Answers[i] = "";
            }
            stuTrueNum = 0;

        }

        public static void TimeCalc()
        {
            int h = 0, m = 0, s = 0;
            int sec = timerHour;
            h = sec / 3600;
            m = sec % 3600;
            m = m / 60;
            s = sec % 3600 % 60;
            timerHour--;
            TimeValue = string.Format("{0}时{1}分{2}秒", h, m, s);
        }

        public static void InitRnd(int id)
        {
            //查询学员所选课程在数据库中的题数
            string sql = "select count(*) from question where subjectid="+id;
            SqlCommand command = new SqlCommand(sql, DataBase.connection);
            int lenght = (int)command.ExecuteScalar();
            int[] qid = new int[lenght];
            //查询并输出所有对应科目的题目ID值到QID数组
            sql = "select QuestionID from Question where subjectid="+id;
            command = new SqlCommand(sql, DataBase.connection);
            SqlDataReader dataReader = command.ExecuteReader();
            int i = 0;
            while (dataReader.Read())
            {
                qid[i] = (int)dataReader[0];
                i++;
            }
            dataReader.Close();
            //随机抽二十题
            int[] abc = new int[20];
            int n = 0;
            int rndValue = 0;
            Boolean DataAgin = false;
            Random rnd = new Random();
            while (n < 19)
            {
                rndValue = rnd.Next(0, lenght);
                DataAgin = false;
                for (int j = 0; j < n; j++)
                {
                    if (rndValue == abc[j])
                    {
                        DataAgin = true;
                        continue;
                    }

                }
                if (!DataAgin)
                {
                    abc[n] = rndValue;
                    n++;
                }
            }

            QID = new int[abc.Length];
            for (int k = 0; k < 20; k++)
            {
                QID[k] = qid[abc[k]];
                Console.WriteLine(QID[k].ToString());
            }
        }
    }
}
